package com.apex.aolonfitdemo.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivitySedentaryBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.NotDisturbDialog;
import com.apex.aolonfitdemo.dialog.SwitchDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.SedentaryCheckCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleSedentariness;
import com.cyber.bridge.module.bean.SedentaryReminderEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;


public class SedentaryActivity extends AppCompatActivity {

    private WaitingDialog waitingDialog;
    private int tempInterval, tempStartHour, tempStartMinute, tempEndHour, tempEndMinute;
    private MonthDialog monthDialog;
    private SedentaryReminderEntity sedentaryReminderEntity;
    private SwitchDialog sitDialog;
    private NotDisturbDialog startDialog, endDialog;


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                sedentaryReminderEntity = (SedentaryReminderEntity) msg.obj;
                if (sedentaryReminderEntity == null) {
                    return;
                }
                tempInterval = sedentaryReminderEntity.getInterval();
                tempEndHour = sedentaryReminderEntity.getEndTime() / 60;
                tempEndMinute = sedentaryReminderEntity.getEndTime() % 60;
                tempStartHour = sedentaryReminderEntity.getStartTime() / 60;
                tempStartMinute = sedentaryReminderEntity.getStartTime() % 60;
                if (!sedentaryReminderEntity.isEnabled()) {
                    activitySedentaryBinding.sitSwitch.setText(getString(R.string.switch_state_close));
                } else {
                    activitySedentaryBinding.sitSwitch.setText(getString(R.string.switch_state_on));
                }
                activitySedentaryBinding.intervalTime.setText(sedentaryReminderEntity.getInterval() + "");
                activitySedentaryBinding.startTime.setText(tempStartHour + ":" + tempStartMinute);
                activitySedentaryBinding.endTime.setText(tempEndHour + ":" + tempEndMinute);
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SedentaryActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SedentaryActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivitySedentaryBinding activitySedentaryBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySedentaryBinding = ActivitySedentaryBinding.inflate(getLayoutInflater());
        setContentView(activitySedentaryBinding.getRoot());

        activitySedentaryBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySedentaryBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySedentaryBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnSedentaryReminderListener(new OnCyberQueryAndSetStateListener<SedentaryReminderEntity>() {
            @Override
            public void queryConfig(SedentaryReminderEntity sedentaryReminderEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = sedentaryReminderEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(SedentaryActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().querySedentaryReminder();
        activitySedentaryBinding.intervalTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (monthDialog == null) {
                    monthDialog = new MonthDialog(SedentaryActivity.this, getString(R.string.interval_time));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            tempInterval = sex;
                            activitySedentaryBinding.intervalTime.setText(tempInterval + "");
                        }
                    });
                }
                if (!monthDialog.isShowing()) {
                    monthDialog.show();
                }

            }
        });
        activitySedentaryBinding.startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (startDialog == null) {
                    startDialog = new NotDisturbDialog(SedentaryActivity.this, getString(R.string.start_time));
                    startDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {
                            tempStartHour = hour;
                            tempStartMinute = minute;
                            activitySedentaryBinding.startTime.setText(tempStartHour + ":" + tempStartMinute);
                        }
                    });
                }
                if (!startDialog.isShowing()) {
                    startDialog.show();
                }
            }
        });
        activitySedentaryBinding.endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (endDialog == null) {
                    endDialog = new NotDisturbDialog(SedentaryActivity.this, getString(R.string.end_time));
                    endDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {
                            tempEndHour = hour;
                            tempEndMinute = minute;
                            activitySedentaryBinding.endTime.setText(tempEndHour + ":" + tempEndMinute);
                        }
                    });
                }
                if (!endDialog.isShowing()) {
                    endDialog.show();
                }
            }
        });
        activitySedentaryBinding.sitSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sitDialog == null) {
                    sitDialog = new SwitchDialog(SedentaryActivity.this);
                    sitDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activitySedentaryBinding.sitSwitch.setText(sex);

                        }
                    });
                }
                if (!sitDialog.isShowing()) {
                    sitDialog.show();
                }

            }
        });

        activitySedentaryBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sitSwitch = activitySedentaryBinding.sitSwitch.getText().toString();
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(SedentaryActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                AolonFitManager.getInstance().setSedentaryReminder(new SedentaryReminderEntity(getString(R.string.switch_state_close).equalsIgnoreCase(sitSwitch) ? false : true, false, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute, tempInterval));

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (sitDialog != null) {
            sitDialog.destroyDialog();
            sitDialog = null;
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (startDialog != null) {
            startDialog.dismiss();
            startDialog.destroyDialog();
            startDialog = null;
        }
        if (endDialog != null) {
            endDialog.dismiss();
            endDialog.destroyDialog();
            endDialog = null;
        }
        super.onDestroy();
    }


}
